package com.babasport.web.action.product;

import java.io.File;
import java.io.FileOutputStream;
import java.util.HashMap;
import java.util.UUID;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import org.apache.struts.upload.FormFile;
import org.springframework.stereotype.Controller;

import com.babasport.bean.PageView;
import com.babasport.bean.QueryResult;
import com.babasport.bean.product.Brand;
import com.babasport.service.product.BrandService;
import com.babasport.web.formbean.product.BrandForm;

@Controller("/control/brand/manage")
public class BrandAction extends DispatchAction {

	private static final Logger logger = Logger.getLogger(BrandAction.class);

	@Resource(name = "brandServiceImpl")
	private BrandService service;

	public ActionForward list(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		BrandForm formBean = (BrandForm) form;
		PageView<Brand> pageView = new PageView<Brand>(12, formBean.getPage());
		int firstIndex = (pageView.getCurrentpage() - 1)
				* pageView.getMaxresult();

		HashMap<String, Object> where = new HashMap<String, Object>();
		where.put("visible=", true);
		String brandName = formBean.getName();
		if(brandName != null && !"".equals(brandName.trim())){
			where.put("name like ", "'%" + brandName + "%'");
		}
		
		QueryResult<Brand> qr = service.getScrollData(Brand.class, firstIndex,
				pageView.getMaxresult(), where);
		pageView.setQueryResult(qr);
		request.setAttribute("pageView", pageView);
		return mapping.findForward("listUI");
	}

	public ActionForward addUI(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		return mapping.findForward("addUI");
	}
	
	public ActionForward add(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		BrandForm formBean = (BrandForm) form;
		service.save(new Brand(formBean.getName(), this.saveLogo(formBean.getLogoFile(), request)));
		request.setAttribute("message", "添加品牌成功!");
		request.setAttribute("urladdress", "/control/brand/manage.do?method=list");
		return mapping.findForward("message");
	}
	
	private String saveLogo(FormFile logoFile,HttpServletRequest request) throws Exception {
		String logoPath = "";
		if(logoFile != null && logoFile.getFileSize() > 0){
			String logoPathDir = request.getSession().getServletContext().getRealPath("/images/brand/");
			File logoSaveFile = new File(logoPathDir);
			if(!logoSaveFile.exists()){
				logoSaveFile.mkdirs();
			}
			String ext = logoFile.getFileName().substring(logoFile.getFileName().lastIndexOf("."));
			String imgName = UUID.randomUUID().toString() + ext;
			FileOutputStream fos = new FileOutputStream(new File(logoPathDir,imgName));
			fos.write(logoFile.getFileData());
			fos.close();
			logoPath = logoPathDir + "/" + imgName;
			logger.debug("LOGO保存在: " + logoPath);
		}
		return logoPath;
	}
	
	public ActionForward editUI(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		BrandForm formBean = (BrandForm) form;
		Brand brand = service.query(Brand.class, formBean.getCode());
		formBean.setName(brand.getName());
		formBean.setLogoPath(brand.getLogoPath());
		return mapping.findForward("editUI");
	}
	
	public ActionForward edit(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		BrandForm formBean = (BrandForm) form;
		Brand brand = service.query(Brand.class, formBean.getCode());
		brand.setName(formBean.getName());
		brand.setLogoPath(this.saveLogo(formBean.getLogoFile(), request));
		service.update(brand);
		request.setAttribute("message", "修改品牌成功!");
		request.setAttribute("urladdress", "/control/brand/manage.do?method=list");
		return mapping.findForward("message");
	}
	
	public ActionForward queryUI(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		return mapping.findForward("queryUI");
	}
	
}
